﻿using System;
using System.Windows.Data;

namespace TheOliver.DigitalSignage.Client.Interfaces
{
    public class TimeConverter : IValueConverter
    {

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = string.Empty;

            if (value == null)
            {
                result = string.Empty;
            }
            else if (parameter == null)
            {
                result = value.ToString();
            }
            else
            {
                TimeSpan ts = (TimeSpan)value;

                DateTime dt = new DateTime();
                dt = dt.Add(ts);

                string formatParameter = parameter.ToString();
                if (!formatParameter.StartsWith("{"))
                {
                    formatParameter = "{0:" + formatParameter;
                }
                if (!formatParameter.EndsWith("}"))
                {
                    formatParameter = formatParameter + "}";
                }
                result = string.Format(formatParameter, dt);
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
